﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace 城市天然气管道风险管理与决策系统
{
    public partial class Form_menu_3 : Form
    {
        private List<int> selected_item;
        private List<double> u_RS;
        private List<double> u_PR;
        private List<double> u_ROC;
        private readonly List<string> solutions = new List<string>() { "方案一", "方案二", "方案三", "方案四", "方案五" };

        public Form_menu_3()
        {
            InitializeComponent();

            selected_item = new List<int>();
            u_PR = new List<double>();
            u_ROC = new List<double>();
            u_RS = new List<double>();
        }

        private void Form_menu_3_Load(object sender, EventArgs e)
        {
            textBox1.Text = GlobalData.default_pipeline.Id.ToString();
            textBox2.Text = GlobalData.default_pipeline.Name;
            textBox4.Text = GlobalData.default_pipeline.StartLocation;
            textBox3.Text = GlobalData.default_pipeline.EndLocation;

            comboBox2.Enabled = false;
            comboBox3.Enabled = false;
            comboBox4.Enabled = false;
            comboBox5.Enabled = false;
            comboBox6.Enabled = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Enabled = true;
            selected_item.Add(comboBox1.SelectedIndex);
            comboBox1.Enabled = false;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!selected_item.Contains(comboBox2.SelectedIndex))
            {
                selected_item.Add(comboBox2.SelectedIndex);
                comboBox3.Enabled = true;
                comboBox2.Enabled = false;
            }
            else
            {
                MessageBox.Show("请勿重复选择");
                return;
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!selected_item.Contains(comboBox3.SelectedIndex))
            {
                selected_item.Add(comboBox3.SelectedIndex);
                comboBox4.Enabled = true;
                comboBox3.Enabled = false;
            }
            else
            {
                MessageBox.Show("请勿重复选择");
                return;
            }
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!selected_item.Contains(comboBox4.SelectedIndex))
            {
                selected_item.Add(comboBox4.SelectedIndex);
                comboBox5.Enabled = true;
                comboBox4.Enabled = false;
            }
            else
            {
                MessageBox.Show("请勿重复选择");
                return;
            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!selected_item.Contains(comboBox5.SelectedIndex))
            {
                selected_item.Add(comboBox5.SelectedIndex);
                comboBox6.Enabled = true;
                comboBox5.Enabled = false;
            }
            else
            {
                MessageBox.Show("请勿重复选择");
                return;
            }
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!selected_item.Contains(comboBox6.SelectedIndex))
            {
                selected_item.Add(comboBox6.SelectedIndex);
                comboBox6.Enabled = false;
            }
            else
            {
                MessageBox.Show("请勿重复选择");
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double u1 = Math.Exp(double.Parse(textBox5.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox6.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox7.Text) / 100;
                double u4 = 1 - double.Parse(textBox8.Text) / 100;
                double u5 = double.Parse(textBox9.Text) / 100;
                double u6 = 1 - double.Parse(textBox10.Text) / 100;
                List<double> utilities = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities = Util.SortByOrder(selected_item, utilities);

                double w1 = Util.RankSum(6, 1);
                double w2 = Util.RankSum(6, 2);
                double w3 = Util.RankSum(6, 3);
                double w4 = Util.RankSum(6, 4);
                double w5 = Util.RankSum(6, 5);
                double w6 = Util.RankSum(6, 6);
                List<double> weights = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility = Util.ComputeUtility(utilities, weights);
                textBox11.Text = utility.ToString("0.000");
                u_RS.Add(utility);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox22.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox21.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox20.Text) / 100;
                double u4 = 1 - double.Parse(textBox19.Text) / 100;
                double u5 = double.Parse(textBox18.Text) / 100;
                double u6 = 1 - double.Parse(textBox17.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankSum(6, 1);
                double w2 = Util.RankSum(6, 2);
                double w3 = Util.RankSum(6, 3);
                double w4 = Util.RankSum(6, 4);
                double w5 = Util.RankSum(6, 5);
                double w6 = Util.RankSum(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox16.Text = utility1.ToString("0.000");
                u_RS.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox31.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox30.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox29.Text) / 100;
                double u4 = 1 - double.Parse(textBox28.Text) / 100;
                double u5 = double.Parse(textBox27.Text) / 100;
                double u6 = 1 - double.Parse(textBox26.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankSum(6, 1);
                double w2 = Util.RankSum(6, 2);
                double w3 = Util.RankSum(6, 3);
                double w4 = Util.RankSum(6, 4);
                double w5 = Util.RankSum(6, 5);
                double w6 = Util.RankSum(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox25.Text = utility1.ToString("0.000");
                u_RS.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox40.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox39.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox38.Text) / 100;
                double u4 = 1 - double.Parse(textBox37.Text) / 100;
                double u5 = double.Parse(textBox36.Text) / 100;
                double u6 = 1 - double.Parse(textBox35.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankSum(6, 1);
                double w2 = Util.RankSum(6, 2);
                double w3 = Util.RankSum(6, 3);
                double w4 = Util.RankSum(6, 4);
                double w5 = Util.RankSum(6, 5);
                double w6 = Util.RankSum(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox34.Text = utility1.ToString("0.000");
                u_RS.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox49.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox48.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox47.Text) / 100;
                double u4 = 1 - double.Parse(textBox46.Text) / 100;
                double u5 = double.Parse(textBox45.Text) / 100;
                double u6 = 1 - double.Parse(textBox44.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankSum(6, 1);
                double w2 = Util.RankSum(6, 2);
                double w3 = Util.RankSum(6, 3);
                double w4 = Util.RankSum(6, 4);
                double w5 = Util.RankSum(6, 5);
                double w6 = Util.RankSum(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox43.Text = utility1.ToString("0.000");
                u_RS.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            double max_u = u_RS.Max();
            int index = u_RS.IndexOf(max_u);
            textBox51.Text = solutions[index];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double u1 = Math.Exp(double.Parse(textBox5.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox6.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox7.Text) / 100;
                double u4 = 1 - double.Parse(textBox8.Text) / 100;
                double u5 = double.Parse(textBox9.Text) / 100;
                double u6 = 1 - double.Parse(textBox10.Text) / 100;
                List<double> utilities = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities = Util.SortByOrder(selected_item, utilities);

                double w1 = Util.PR(6, 1);
                double w2 = Util.PR(6, 2);
                double w3 = Util.PR(6, 3);
                double w4 = Util.PR(6, 4);
                double w5 = Util.PR(6, 5);
                double w6 = Util.PR(6, 6);
                List<double> weights = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility = Util.ComputeUtility(utilities, weights);
                textBox12.Text = utility.ToString("0.000");
                u_PR.Add(utility);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox22.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox21.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox20.Text) / 100;
                double u4 = 1 - double.Parse(textBox19.Text) / 100;
                double u5 = double.Parse(textBox18.Text) / 100;
                double u6 = 1 - double.Parse(textBox17.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.PR(6, 1);
                double w2 = Util.PR(6, 2);
                double w3 = Util.PR(6, 3);
                double w4 = Util.PR(6, 4);
                double w5 = Util.PR(6, 5);
                double w6 = Util.PR(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox15.Text = utility1.ToString("0.000");
                u_PR.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox31.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox30.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox29.Text) / 100;
                double u4 = 1 - double.Parse(textBox28.Text) / 100;
                double u5 = double.Parse(textBox27.Text) / 100;
                double u6 = 1 - double.Parse(textBox26.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.PR(6, 1);
                double w2 = Util.PR(6, 2);
                double w3 = Util.PR(6, 3);
                double w4 = Util.PR(6, 4);
                double w5 = Util.PR(6, 5);
                double w6 = Util.PR(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox24.Text = utility1.ToString("0.000");
                u_PR.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox40.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox39.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox38.Text) / 100;
                double u4 = 1 - double.Parse(textBox37.Text) / 100;
                double u5 = double.Parse(textBox36.Text) / 100;
                double u6 = 1 - double.Parse(textBox35.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.PR(6, 1);
                double w2 = Util.PR(6, 2);
                double w3 = Util.PR(6, 3);
                double w4 = Util.PR(6, 4);
                double w5 = Util.PR(6, 5);
                double w6 = Util.PR(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox33.Text = utility1.ToString("0.000");
                u_PR.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox49.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox48.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox47.Text) / 100;
                double u4 = 1 - double.Parse(textBox46.Text) / 100;
                double u5 = double.Parse(textBox45.Text) / 100;
                double u6 = 1 - double.Parse(textBox44.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.PR(6, 1);
                double w2 = Util.PR(6, 2);
                double w3 = Util.PR(6, 3);
                double w4 = Util.PR(6, 4);
                double w5 = Util.PR(6, 5);
                double w6 = Util.PR(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox42.Text = utility1.ToString("0.000");
                u_PR.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            double max_u = u_PR.Max();
            int index = u_PR.IndexOf(max_u);
            textBox50.Text = solutions[index];
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //var text = "没有公式";
            //textBox13.Text = text;
            //textBox14.Text = text;
            //textBox23.Text = text;
            //textBox32.Text = text;
            //textBox42.Text = text;
            //textBox52.Text = text;

            try
            {
                double u1 = Math.Exp(double.Parse(textBox5.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox6.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox7.Text) / 100;
                double u4 = 1 - double.Parse(textBox8.Text) / 100;
                double u5 = double.Parse(textBox9.Text) / 100;
                double u6 = 1 - double.Parse(textBox10.Text) / 100;
                List<double> utilities = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities = Util.SortByOrder(selected_item, utilities);

                double w1 = Util.RankOrderCentroid(6, 1);
                double w2 = Util.RankOrderCentroid(6, 2);
                double w3 = Util.RankOrderCentroid(6, 3);
                double w4 = Util.RankOrderCentroid(6, 4);
                double w5 = Util.RankOrderCentroid(6, 5);
                double w6 = Util.RankOrderCentroid(6, 6);
                List<double> weights = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility = Util.ComputeUtility(utilities, weights);
                textBox13.Text = utility.ToString("0.000");
                u_ROC.Add(utility);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox22.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox21.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox20.Text) / 100;
                double u4 = 1 - double.Parse(textBox19.Text) / 100;
                double u5 = double.Parse(textBox18.Text) / 100;
                double u6 = 1 - double.Parse(textBox17.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankOrderCentroid(6, 1);
                double w2 = Util.RankOrderCentroid(6, 2);
                double w3 = Util.RankOrderCentroid(6, 3);
                double w4 = Util.RankOrderCentroid(6, 4);
                double w5 = Util.RankOrderCentroid(6, 5);
                double w6 = Util.RankOrderCentroid(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox14.Text = utility1.ToString("0.000");
                u_ROC.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox31.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox30.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox29.Text) / 100;
                double u4 = 1 - double.Parse(textBox28.Text) / 100;
                double u5 = double.Parse(textBox27.Text) / 100;
                double u6 = 1 - double.Parse(textBox26.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankOrderCentroid(6, 1);
                double w2 = Util.RankOrderCentroid(6, 2);
                double w3 = Util.RankOrderCentroid(6, 3);
                double w4 = Util.RankOrderCentroid(6, 4);
                double w5 = Util.RankOrderCentroid(6, 5);
                double w6 = Util.RankOrderCentroid(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox23.Text = utility1.ToString("0.000");
                u_ROC.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox40.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox39.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox38.Text) / 100;
                double u4 = 1 - double.Parse(textBox37.Text) / 100;
                double u5 = double.Parse(textBox36.Text) / 100;
                double u6 = 1 - double.Parse(textBox35.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankOrderCentroid(6, 1);
                double w2 = Util.RankOrderCentroid(6, 2);
                double w3 = Util.RankOrderCentroid(6, 3);
                double w4 = Util.RankOrderCentroid(6, 4);
                double w5 = Util.RankOrderCentroid(6, 5);
                double w6 = Util.RankOrderCentroid(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox32.Text = utility1.ToString("0.000");
                u_ROC.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            try
            {
                double u1 = Math.Exp(double.Parse(textBox49.Text) * 0.0085) * 0.22 - 0.22;
                double u2 = Math.Exp(double.Parse(textBox48.Text) * 0.00002) * 0.27 - 0.27;
                double u3 = double.Parse(textBox47.Text) / 100;
                double u4 = 1 - double.Parse(textBox46.Text) / 100;
                double u5 = double.Parse(textBox45.Text) / 100;
                double u6 = 1 - double.Parse(textBox44.Text) / 100;
                List<double> utilities1 = new List<double>() { u1, u2, u3, u4, u5, u6 };

                utilities1 = Util.SortByOrder(selected_item, utilities1);

                double w1 = Util.RankOrderCentroid(6, 1);
                double w2 = Util.RankOrderCentroid(6, 2);
                double w3 = Util.RankOrderCentroid(6, 3);
                double w4 = Util.RankOrderCentroid(6, 4);
                double w5 = Util.RankOrderCentroid(6, 5);
                double w6 = Util.RankOrderCentroid(6, 6);
                List<double> weights1 = new List<double>() { w1, w2, w3, w4, w5, w6 };

                double utility1 = Util.ComputeUtility(utilities1, weights1);
                textBox41.Text = utility1.ToString("0.000");
                u_ROC.Add(utility1);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不完整");
                return;
            }

            double max_u = u_ROC.Max();
            int index = u_ROC.IndexOf(max_u);
            textBox52.Text = solutions[index];
        }

        private void button4_Click(object sender, EventArgs e)
        {
            selected_item.Clear();
            comboBox1.Enabled = true;
            comboBox2.Enabled = false;
            comboBox3.Enabled = false;
            comboBox4.Enabled = false;
            comboBox5.Enabled = false;
            comboBox6.Enabled = false;
            comboBox1.Text = "";
            comboBox2.Text = "";
            comboBox3.Text = "";
            comboBox4.Text = "";
            comboBox5.Text = "";
            comboBox6.Text = "";
            comboBox1.Focus();
        }
    }
}
